<script setup>

import {ref, onMounted, watch, computed} from 'vue';
import axios from "axios";
import {snackbar} from './store'

// 查询
const itemsPerPage = ref(50);
const headers = [
    {title: '序号', key: 'num', sortable: false},
    {title: '菜单名称', key: 'menuName', sortable: false},
    {title: '菜单类型', key: 'menuType', sortable: false},
    {title: '路径', key: 'path', sortable: false},
    {title: '标识符号', key: 'icon', sortable: false},
    {title: '排序', key: 'orderNum', sortable: false},
    {title: '状态', key: 'status', sortable: false},
    {title: '操作', key: 'actions', sortable: false}
];
const items = ref([]);
const loading = ref(true);
const totalItems = ref(0);

const search = ref('');
const name = ref(null);
watch(name, () => {
    search.value = name.value
})

function loadItems({page, itemsPerPage}) {
    loading.value = true;
    axios.post('/sys_menu/sys-menu/selectPage', {
        pageNo: page,
        pageSize: itemsPerPage,
        menuName: name.value === null ? null : name.value,
        delFlag: '0'
    }).then(({data}) => {
        items.value = data.data.records;
        items.value.forEach(item => {
            if(item.parentId == 0) {
                item.parentName = '系统管理'
            }
            if(item.parentId == 1) {
                item.parentName = '网格化巡检'
            }
            if(item.parentId == 2) {
                item.parentName = '消防巡检'
            }
            if(item.parentId == 3) {
                item.parentName = '其他报警'
            }
            if(item.parentId == 4) {
                item.parentName = '舆情管理'
            }
            if(item.parentId == 6) {
                item.parentName = '玻璃巡检'
            }
        })
        totalItems.value = data.data.total;
        loading.value = false;
    })
}

onMounted(() => {
    loadItems({page: 1, itemsPerPage: itemsPerPage.value});
});

// 增改
const isShowDialog = ref(false)
const dialogEnum = {
    update: '编辑',
    create: '新增'
}
const dialogType = ref(null)
const isShowDeleteDialog = ref(false)   
const editedItem = ref({
    menuName: "",
    status: "0",
    icon: "",
    path: "",
    orderNum: ""
});

function resetEditedItem() {
    editedItem.value = {
        menuName: "",
        status: "0",
        icon: "",
        path: "",
        orderNum: ""
    }
}

function clickNew() {
    dialogType.value = dialogEnum.create
    isShowDialog.value = true
}

function close() {
    isShowDialog.value = false
    resetEditedItem()
    dialogType.value = null
}

function clickEdit(item) {
    editedItem.value = Object.assign({}, item)
    isShowDialog.value = true
    dialogType.value = dialogEnum.update
}

function save() {
    if (dialogType.value === dialogEnum.update) {
        axios({
            method: 'POST',
            url: '/sys_menu/sys-menu/updateMenu',
            data: editedItem.value
        }).then(() => {
            snackbar.info('编辑成功')
            close()
            loadItems({page: 1, itemsPerPage: itemsPerPage.value})
        })
    } else if (dialogType.value === dialogEnum.create) {
        axios({
            method: 'POST',
            url: '/sys_menu/sys-menu/saveMenu',
            data: editedItem.value
        }).then((res) => {
            console.log(res.data)
            snackbar.info('创建成功')
            close()
            loadItems({page: 1, itemsPerPage: itemsPerPage.value})
        })
    }
}

// 删
const beDeleteItem = ref({})

function clickDelete(item) {
    isShowDeleteDialog.value = true
    beDeleteItem.value = item
}

function clickDeleteConfirm() {
    axios.get('/sys_role/role/delRole/?roleId=' + beDeleteItem.value.id).then(() => {
        snackbar.info("删除成功")
        loadItems({page: 1, itemsPerPage: itemsPerPage.value})
        clickCloseDelete()
    })
}

function clickCloseDelete() {
    isShowDeleteDialog.value = false
    beDeleteItem.value = null
}

</script>

<template>
    <v-toolbar density="compact">
        <v-toolbar-title>菜单管理</v-toolbar-title>
        <v-text-field v-model="name" :clearable="true" hide-details placeholder="查询名称"></v-text-field>
        <v-dialog v-model="isShowDialog" max-width="500px">
            <template v-slot:activator="{ props }">
                <v-btn icon="mdi-plus" v-on:click="clickNew" v-bind="props"></v-btn>
            </template>
            <v-card>
                <v-card-title>{{ dialogType }}</v-card-title>
                <v-card-text>
                    <v-text-field v-model="editedItem.menuName" label="菜单名称" required></v-text-field>
                    <v-text-field v-model="editedItem.menuType" label="菜单类型" required></v-text-field>
                    <v-text-field v-model="editedItem.path" label="路径" required></v-text-field>
                    <v-text-field v-model="editedItem.icon" label="标识符号" required></v-text-field>
                    <v-text-field v-model="editedItem.orderNum" label="排序" required></v-text-field>
                    <v-select v-model="editedItem.parentId" label="父级目录" :items="[{value:0, title: '系统管理'},{value:1, title: '网格化巡检'},{value:2, title: '消防巡检'},{value:3, title: '其他报警'},{value:4, title: '舆情管理'},{value:6, title: '玻璃巡检'}]" required></v-select>
                    <v-select v-model="editedItem.status" :items="[{value:'0',title:'正常'},{value:'1',title:'停用'}]"
                    label="菜单状态"></v-select>
                </v-card-text>
                <v-card-actions>
                    <v-btn color="primary" variant="text" @click="close">关闭</v-btn>
                    <v-btn color="primary" variant="text" @click="save">确定</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <v-dialog v-model="isShowDeleteDialog" max-width="500px">
            <v-card>
                <v-card-title class="text-h5">确定删除?</v-card-title>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn color="blue-darken-1" variant="text" @click="clickCloseDelete">取消</v-btn>
                    <v-btn color="blue-darken-1" variant="text" @click="clickDeleteConfirm">确定</v-btn>
                    <v-spacer></v-spacer>
                </v-card-actions>
            </v-card>
        </v-dialog>
    </v-toolbar>
  <!--  分页文本待更换   items-per-page-text="每页数量"-->
    <v-data-table-server
    :group-by="[{key:'parentName'}]"
    :search="search"
    :items-per-page="itemsPerPage"
    :headers="headers"
    :items-length="totalItems"
    :items="items"
    :loading="loading"
    class="elevation-1"
    items-per-page-text="每页数量"
    @update:options="loadItems">
        <template v-slot:item.num="{ index }">
            <span>{{ index + 1 }}</span>
        </template>
        <template v-slot:item.status="{ item }">
            <span v-if="item.raw.status == 0">正常</span>
            <span v-if="item.raw.status == 1">停用</span>
        </template>
        <template v-slot:item.actions="{ item }">
            <v-btn icon="mdi-pencil" variant="text" @click="clickEdit(item.raw)"></v-btn>
            <v-btn icon="mdi-delete" variant="text" @click="clickDelete(item.raw)"></v-btn>
        </template>
    </v-data-table-server>
</template>

<style scoped lang="less">

</style>
